<template>
  <el-dialog title="编辑活动楼盘" :visible.sync="dialogVisible" width="500px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="城市：" prop="cityId">
        <el-select v-model="ruleForm.cityId" @change="cityChangeHandle" filterable>
          <el-option v-for="item in cityList" :label="item.city" :value="item.cityId" :key="item.cityId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼盘名称：" prop="devId">
        <el-select v-model="ruleForm.devId" @change="devChangeHandle" filterable>
          <el-option v-for="item in houseList" :label="item.devName" :value="item.devId" :key="item.devId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图片：">
        <ImgView v-model="ruleForm.imgUrl"></ImgView>
      </el-form-item>
      <el-form-item label="推荐理由：" prop="recommendReason">
        <el-input v-model="ruleForm.recommendReason" type="textarea" :rows="4" placeholder="200字符以内" maxlength="200"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import api from '@/api/apiPath'
  import ImgView from '@/components/common/MultipleImgView'
  export default {
    name: "EditBanner",
    components: {ImgView},
    data(){
      return {
        dialogVisible: false,
        cityList: [],
        houseList: [],
        ruleForm: {
          cityId: '',
          devId: '',
          recommendReason: '',
          imgUrl: '',
          build: null,
        },
        rules: {
          cityId: [
            { required: true, message: '请选择城市', trigger: 'change' },
          ],
          devId: [
            { required: true, message: '请选择楼盘', trigger: 'change' }
          ],
          recommendReason: [
            {required: true, message: '请输入推荐理由', trigger: 'blur'}
          ]
        }
      }
    },
    props: ['house'],
    created(){
      this.searchCityList()
    },
    methods: {
      // 查询城市列表
      searchCityList(){
        this.axios.post(api.CITYLIST).then(res => {
          this.cityList = res.data.data.cityList
        })
      },
      // 根据城市ID查询楼盘列表
      cityChangeHandle (cityId) {
        this.ruleForm.devId = ''
        this.axios.post(api.HOMESEARCH+'?pageNum=1', {cityId: cityId,pageSize:9999}).then(res => {
          const data = res.data
          this.houseList = data.data
        })
      },
      // 选了楼盘
      devChangeHandle (val) {
        const dev = this.houseList.find(item => {
          return item.devId == val
        })
        let temp = dev.effectId.length>0? dev.effectId[0].url: ''
        this.ruleForm.imgUrl = temp
        this.ruleForm.build = dev
      },
      show(){
        this.dialogVisible = true
        this.$nextTick(() => {
          this.ruleForm = this.house
          if (this.ruleForm.cityId) {
            this.axios.post(api.HOMESEARCH+'?pageNum=1', {cityId: this.ruleForm.cityId,pageSize:9999}).then(res => {
              this.houseList = res.data.data
            })
          }
        })
      },
      addShow(){
        this.dialogVisible = true
        this.$nextTick(() => {
          this.resetForm()
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$emit('success',this.ruleForm)
            this.dialogVisible = false
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm() {
        this.$refs['ruleForm'].resetFields();
        this.ruleForm.imgUrl = ''
      }
    }
  }
</script>

<style scoped>

</style>
